package com.afd.ozblip.share;

import com.afd.ozblip.util.StringUtil;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

public class TokenStore {

	public static String fileName = "OZBLIP_TOKEN_";

	/**
	 * 存储Token
	 * 
	 * @param activity
	 * @param oauth
	 */
	public static boolean storeOAuth(Activity activity, OAuth oauth) {
		if (oauth == null) {
			return false;
		}
		try {
			String weiboType = oauth.getWeiboType();
			if (StringUtil.isNullOrEmpty(weiboType)) {
				return false;
			}
			SharedPreferences settings = activity.getSharedPreferences(fileName
					+ weiboType, Context.MODE_PRIVATE);
			SharedPreferences.Editor editor = settings.edit();
			editor.putString("oauth_token", oauth.getAccessToken());
			editor.putString("respones_data", oauth.getResponseData());
			editor.commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}

	/**
	 * 存储用户信息
	 * 
	 * @param activity
	 * @param oauth
	 */
	public static boolean storeUserInfo(Activity activity,
			WeiBoUserEntity userInfo, String weiboType) {
		if (userInfo == null) {
			return false;
		}
		try {
			if (StringUtil.isNullOrEmpty(weiboType)) {
				return false;
			}
			SharedPreferences settings = activity.getSharedPreferences(fileName
					+ weiboType, Context.MODE_PRIVATE);
			SharedPreferences.Editor editor = settings.edit();
			editor.putString("user_name", userInfo.getUserName());
			editor.commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}

	/**
	 * 获取Token
	 * 
	 * @param context
	 * @param weiboType
	 * @return
	 */
	public static OAuth fetchOAuth(Context context, String weiboType) {
		if (StringUtil.isNullOrEmpty(weiboType)) {
			return null;
		}
		SharedPreferences settings = context.getSharedPreferences(fileName
				+ weiboType, Context.MODE_PRIVATE);
		String oauth_token = settings.getString("oauth_token", null);
		String responseData = settings.getString("respones_data", null);
		OAuth oauth = null;
		if(oauth_token!=null&&responseData!=null){
			oauth = new OAuth();
			oauth.setAccessToken(oauth_token);
			oauth.setResponseData(responseData);
		}
		return oauth;
	}

	/**
	 * 获取获取用户信息
	 * 
	 * @param activity
	 * @param weiboType
	 * @return
	 */
	public static WeiBoUserEntity fetchUser(Activity activity, String weiboType) {
		if (StringUtil.isNullOrEmpty(weiboType)) {
			return null;
		}
		SharedPreferences settings = activity.getSharedPreferences(fileName
				+ weiboType, Context.MODE_PRIVATE);
		String userName = settings.getString("user_name", null);
		WeiBoUserEntity userInfo = new WeiBoUserEntity();
		userInfo.setUserName(userName);
		return userInfo;
	}

	/**
	 * 清除Token
	 * 
	 * @param activity
	 * @param weiboType
	 * @return
	 */
	public static void clear(Activity activity, String weiboType) {
		if (StringUtil.isNullOrEmpty(weiboType)) {
			return;
		}
		SharedPreferences settings = activity.getSharedPreferences(fileName
				+ weiboType, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.clear();
		editor.commit();
	}
}
